
var logmax;
var logmin;
$("#form-member").validate({
    rules: {
        category_id: {
            maxlength:10,
            minlength:10,
        },
        logmax: {
            maxlength:30,
        },
        logmin: {
            maxlength:10,
            minlength:10,
        },
        shaixuan: {
            maxlength:10,
            minlength:10,
        }
    },
    //提交表单数据
    submitHandler:function(form){
        //获取
         logmax=$("#logmax").val(),
         logmin=$("#logmin").val(),
         status=$("#shaixuan").val()
            /*shaixuan=$("#shaixuan").val()*/
            /*text=$("#text").val()*/
        ;
        //提交

        var user_data = {
            effective_date:logmin,
            expiry_date:logmax,
            use_status:status
        }
        //获取返回的数据
        var succCallBack = function (data, status, response) {
            console.log(data)
            if(data.errorCode=="0") {
                var dataSource = new Array();
                var table;   //table对象
                dataSource = [];
                var result=data.data
                $.each(result, function (i, field){
                    var rowData = new Array();
                    //活动标题-0
                    rowData.push("大客户价");
                    //价格基数 1
                    rowData.push("批发价");
                    //折扣率 2
                    rowData.push(field.discount_value+"%");
                    //生效时间 3
                    rowData.push(field.effective_date);
                    //状态 4
                    rowData.push(field.UseStatus);
                    //创建人-5
                    rowData.push(field.creation_by);
                    //创建时间-6
                    rowData.push(field.creation_date);
                    //7
                    rowData.push(field.customer_price_id);
                    //8
                    rowData.push(field.discount_value);
                    dataSource.push(rowData);
                });
                console.log(dataSource)
                initDataTable(dataSource);
                return false
            }else{
                //报错
                console.log("123")
                var dttable = $('#datatable').dataTable( ); // 初始化dataTable，清空上一个值
                dttable.fnClearTable(); //清空一下table
                return false
            }
        };
        //给一个接口地址提交数据
        return web.doAjax("http://192.168.0.53:8580/product/customerPrice/setCustomerPrice", 'post', user_data, succCallBack);
        //获取数据之后如何展现在页面上
        function initDataTable(dataSource) {
            var dttable = $('#datatable').dataTable( ); // 初始化dataTable，清空上一个值
            dttable.fnClearTable(); //清空一下table
            dttable.fnDestroy(); //还原初始化了的datatable
            $("#datatable").DataTable({
                "autoWidth": false,
                "order": [[ 6, "desc" ]],
                "ordering": true,
                "searching": true,
                "paging": true,
                "retrieve": true,
                "destroy": true,
                "bAutoWidth": true,
                "bLengthChange": true,
                "aLengthMenu":[10,20,30],
                "oLanguage": {
                    "sLengthMenu": "每页显示 _MENU_ 条记录",
                    "sZeroRecords": "抱歉， 没有找到",
                    "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                    "sInfoEmpty": "没有数据",
                    "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                    "oPaginate": {
                        "sFirst": "首页",
                        "sPrevious": "前一页",
                        "sNext": "后一页",
                        "sLast": "尾页"
                    },
                    "sZeroRecords": "没有检索到数据",
                    "sProcessing": "<img src='./loading.gif' />"
                },
                data: dataSource,
                columnDefs: [
                    {
                        "visible": true,
                        "targets": 0
                    },
                    {
                        "render": function(data,type,row,meta){
                            if(row[4]==0){
                                var val='<span>未生效</span>'
                            }else if(row[4]==1){
                                var val='<span>生效</span>'
                            } else{
                                var val='<span>失效</span>'
                            }
                            return val;
                        },
                        "targets": 4
                    },
                    {
                        "render": function(data,type,row,meta){
                            if (row[4]==1){
                                var val=""
                               return val;
                            }
                            var val ='<a><i class="Hui-iconfont mr-10" onclick="edit_data('+row[7]+','+row[8]+',\''+row[3]+'\')">&#xe6df;</i></a> <a><i class="Hui-iconfont" onclick="delete_data('+row[7]+')">&#xe6e2;</i></a>';
                            return val;
                        },
                        "targets": 7
                    },
                ]
            });
        };

    }
})

function delete_data(id){
    layer.confirm('你确定要删除吗？',function(index){
        //此处请求后台程序，下方是成功后的前台处理……
        $.ajax({
            contentType: 'application/json',
            url: 'http://192.168.0.53:8580/product/customerPrice/delCustomerPrice',
            type: 'POST',
            data: JSON.stringify({
                customer_price_id:id
            }),
            success: function (data) {
                console.log(data);
                if(data.errorCode=="0") {
                    layer.msg('操作成功!', {icon: 6, time: 1000});
                    setTimeout( function () {
                        location.replace("../operation_product/product.html");
                    },700);
                }
            }
        });
    });
}
/*
下拉框的展示
 */
$(function () {
    $.ajax({
        type: "post",
        url: "http://192.168.0.53:8380/operation/get_category.jhtml",
        cache: false,  //禁用缓存
        dataType: "json",
        data: {category_id:19,type:101},  //传入组装的参数
        success: function (data) {
            if(data.errorCode=="0") {
                var dataInter=data.data;
                var interText = doT.template($("#ascription_bob").text());
                $("#category_id").html(interText(dataInter));
            }
            $.each(dataInter,function(inx,data2){
                if(data2.name=="大客户价配置"){
                    var member='<option selected value="'+data2.category_id+'" >'+data2.name+'</option>';
                    $('#category_id').append(member)
                }else {
                    var member='<option value="'+data2.category_id+'" >'+data2.name+'</option>';
                    $('#category_id').append(member)
                }
            })
        }
    })
})
/*编辑*/
function edit_data(customer_price_id,discount_value,effective_date){
    var index = layer.open({
        type: 2,
        title:"编辑",
        content: "product_edit.html"+"?customer_price_id="+customer_price_id+"&discount_value="+discount_value+"&effective_date="+effective_date
    });
    layer.full(index);
}
//跳转
function discount() {
    $("#category_id").change(function(event) {
        var fanx = $(this).find("option:selected");
        if(fanx.val()==20) {
            window.location.href = "../operation_product/discount.html"
        }
    });
}
// function edit_data(product_management_id,title,release_time){
//     var index = layer.open({
//         type: 2,
//         title:"编辑",
//         product: "product_edit.html"+"?product_management_id="+product_management_id+"&title="+title+"&release_time="+release_time
//     });
//     layer.full(index);
// }
/*
下拉框的展示
 */
// $(function () {
//    // var category_id=$("#category_id").val();
//     $.ajax({
//         type: "post",
//         url: "http://192.168.8.249:8088/operation/get_product_category.jhtml",
//         cache: false,  //禁用缓存
//         dataType: "json",
//         data: {
//             category_id:27,
//             type:105
//         },  //传入组装的参数
//         success: function (data) {
//             if(data.errorCode=="0") {
//                 var dataInter=data.data;
//                 var interText = doT.template($("#ascription_bob").text());
//                 $("#category_id").html(interText(dataInter));
//             }
//         }
//     })
// })